﻿/*
 * "Euphonics" - MainForm.cs
 * Author: Phu H. Le.
 * Version: 1.0.1.
 * Date: 07/02/2009
 * phule@mail.com
 * Copyright©2009 Phu H. Le.
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Euphonics
{
    public partial class MainForm : Form
    {
        private string fileName;                       // Full path name of song.
        private string songName;                       // Just name of song and extension.
        private short  playListIndex            = -1;
        private short  Player                   = 0;
        private short  minVolume                = 0;
        private short  defaultVolume            = 50;
        private short  maxVolume                = 100;
        private short  bandsWidth               = 5;   // Width of each column in spectrum.
        private short  scopeLineWidth           = 4;   // Width of line for oscilloscope.
        private short  zoomPercent              = 100; // Height of spectrum bands.
        private bool   playListActive           = true;
        private bool   loop                     = false;
        private bool   shuffle                  = false;
        private float  bandwidth                = 12;  // Used in equalizer methods.
        private bool   showPlayListAndEqualizer = false;
        private short  smallWidth               = 258; // Default width of the main form.
        private short  bigWidth                 = 650; // Width of main window with playlist and equalizer.

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            short devicesCount = (short)audioDjStudio1.GetOutputDevicesCount();
           
            audioDjStudio1.InitSoundSystem(devicesCount, 0, 0, 0, 0);

            audioDjStudio1.SetPlayerVolume(Player, defaultVolume);

            audioDjStudio1.PlayListCreate(Player);

            show_Spectrum();

            createEqualizer();

            this.Width = smallWidth;
            hidePlayList();
            hideEqualizer();

            // Handle the situation when a file type is automatically
            // associated with Euphonics, and the user double clicks on
            // that file in order to play it, or load it (as is the case
            // for playlists).
            string[] args = Environment.GetCommandLineArgs();

            if (args.Length == 2)
            {
                // args[0] is the name of application
                // while args[1] is the name of the file being clicked
                // and passed in.
                process(sender, e, args[1]);
            }
        }

        private void process(object sender, EventArgs e, string inFile)
        {
            // Get the extension for the file being passed in.
            int index = inFile.LastIndexOf('.');
            string extension = inFile.Substring(index + 1);
            
            switch (extension)
            {
                case "m3u": // Playlist
                    loadPlaylist(sender, e, inFile);
                    break;
                case "pls": // Playlist
                    loadPlaylist(sender, e, inFile);
                    break;
                case "wpl": // Playlist
                    loadPlaylist(sender, e, inFile);
                    break;
                default:    // Audio file
                    playSong(inFile);
                    break;
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            AudioDjStudio.enumPlayerStatus status = audioDjStudio1.GetPlayerStatus(Player);

            if (status != AudioDjStudio.enumPlayerStatus.SOUND_NONE)
            {
                getDuration_Info(sender, e);
            }
        }

        private void volumeTrackBar_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.SetPlayerVolume(Player, (short)volumeTrackBar.Value);
        }

        private void getDuration_Info(object sender, EventArgs e)
        {
            string duration = audioDjStudio1.GetFormattedSoundDuration(Player);
            int index = duration.LastIndexOf('.');
       
            currentPosLabel.Text = audioDjStudio1.GetCurrentPosString(Player, false, false) + " / " + duration.Remove(index + 1);

            int percent = audioDjStudio1.GetCurrentPercentage(Player);

            if (percent < 0)
            {
                return;
            }
            else
            {
                progressTrackBar.Value = percent;
                progressPercentLabel.Text = progressTrackBar.Value.ToString() + "%";
            }
        }

        private void progressTrackBar_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.ScaleUnits = AudioDjStudio.enumScaleUnits.SCALE_PERCENTAGE;
            audioDjStudio1.SeekSound(Player, (uint) progressTrackBar.Value);
        }

        private void show_Spectrum()
        {
            audioDjStudio1.DisplaySpectrum.Create(Player, visualFeedbackPictureBox.Handle);
            audioDjStudio1.DisplaySpectrum.set_ColorBackground(Player, Color.Black);
            audioDjStudio1.DisplaySpectrum.set_ColorBands(Player, Color.MediumBlue);
            audioDjStudio1.DisplaySpectrum.set_ColorPeaks(Player, Color.White);
            audioDjStudio1.DisplaySpectrum.set_BandsWidth(Player, bandsWidth);
            audioDjStudio1.DisplaySpectrum.set_ZoomPerc(Player, zoomPercent);
                        
            audioDjStudio1.DisplaySpectrum.Show(Player, true);
        }

        private void show_Oscilloscope()
        {
            audioDjStudio1.DisplayOscilloscope.Create(Player, visualFeedbackPictureBox.Handle);
            audioDjStudio1.DisplayOscilloscope.set_Type(Player, AudioDjStudio.enumOscillType.OSCILL_TYPE_LINES);
            audioDjStudio1.DisplayOscilloscope.set_ColorLine(Player, Color.White);
            audioDjStudio1.DisplayOscilloscope.set_LineWidth(Player, scopeLineWidth);
            audioDjStudio1.DisplayOscilloscope.set_ZoomPerc(Player, zoomPercent);
            
            audioDjStudio1.DisplayOscilloscope.Show(Player, true);            
        }
           
        private void display_SongInfo(string name)
        {            
            titleTextBox.Text = name;
        }

        private void fillPlayListView()
        {
            string titleStr = audioDjStudio1.PlayListGetItemString(Player, playListIndex, AudioDjStudio.enumPlayListStrings.PL_STRING_INFO_TITLE);
            string durationStr = audioDjStudio1.PlayListGetItemString(Player, playListIndex, AudioDjStudio.enumPlayListStrings.PL_STRING_INFO_FORMATTED_DURAT_NO_MS);

            if (titleStr.Length == 0)
            {
                titleStr = audioDjStudio1.PlayListGetItemString(Player, playListIndex, AudioDjStudio.enumPlayListStrings.PL_STRING_FILENAME);
            }

            string[] stringList = new string[] { titleStr, durationStr };

            ListViewItem item = new ListViewItem(stringList);
            playListView.Items.Add(item);
        }

        private void openButtonPictureBox_Click(object sender, EventArgs e)
        {
            try
            {
                playListActive = false;

                openSongFileDialog.Filter =
                "Supported song formats (*.mp3;*.cda;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3)|" +
                "*.mp3;*.cda;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3|" +
                "MP3 and MP2 songs (*.mp3;*.mp2)|*.mp3;*.mp2|" +
                "CD tracks (*.cda)|*.cda|" +
                "AAC and MP4 songs (*.aac;*.mp4)|*.aac;*.mp4|" +
                "OGG Vorbis songs (*.ogg)|*.ogg|" +
                "AIFF songs (*.aiff)|*.aiff|" +
                "AC3 songs (*.ac3)|*.ac3;|" +
                "ALAC songs (*.m4a)|*.ac3;|" +
                "FLAC songs (*.flac)|*.flac;|" +
                "MOD music (*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3)|*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3|" +
                "WAV songs (*.wav)|*.wav|" +
                "WavPack songs (*.wv)|*.wv;|" +
                "Windows Media songs (*.wma;*.wmv;*.asx;*.asf)|*.wma;*.wmv;*.asx;*.asf|" +
                "All files (*.*)|*.*";

                int index;
               
                if (openSongFileDialog.ShowDialog() == DialogResult.OK)
                {
                    fileName = openSongFileDialog.FileName;
                    index = fileName.LastIndexOf('\\');
                    songName = fileName.Substring(index + 1);
                }
                else //if (openSongFileDialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                if (fileName != null)
                {
                    audioDjStudio1.LoadSound(Player, fileName);
                    audioDjStudio1.PlaySound(Player);
                    
                    if (songName != null)
                    {
                        display_SongInfo(songName);
                    }
                    else
                    {
                        display_SongInfo(fileName);
                    }
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry, Euphonics does not support that particular file. Please choose another song!" + " (The technical problem is: " + ex.Message + ".)");
            } 
        }

        private void openButtonPictureBox_MouseEnter(object sender, EventArgs e)
        {
            openButtonPictureBox.Image = global::Euphonics.Properties.Resources.open_selected_32_32;        
        }

        private void openButtonPictureBox_MouseLeave(object sender, EventArgs e)
        {
            openButtonPictureBox.Image = global::Euphonics.Properties.Resources.open_normal_32_32;
        }

        private void playButtonPictureBox_MouseEnter(object sender, EventArgs e)
        {
            playButtonPictureBox.Image = global::Euphonics.Properties.Resources.play_selected_32_32;
        }

        private void playButtonPictureBox_MouseLeave(object sender, EventArgs e)
        {
            playButtonPictureBox.Image = global::Euphonics.Properties.Resources.play_normal_32_32;
        }

        private void playButtonPictureBox_Click(object sender, EventArgs e)
        {
            try
            {
                AudioDjStudio.enumPlayerStatus status = audioDjStudio1.GetPlayerStatus(Player);

                if (status != AudioDjStudio.enumPlayerStatus.SOUND_NONE)
                {
                    if (status == AudioDjStudio.enumPlayerStatus.SOUND_PAUSED)
                    {
                        audioDjStudio1.ResumeSound(Player);
                        return;
                    }                    
                }
                                             
                if (this.songName == null) // No song has been loaded yet.
                {
                    openButtonPictureBox_Click(sender, e);
                }
                else
                {
                    audioDjStudio1.PlaySound(Player);
                    display_SongInfo(this.songName);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry, Euphonics does not support that particular file. Please choose another song!" + " (The technical problem is: " + ex.Message + ".)");
            }
        }

        private void pauseButtonPictureBox_Click(object sender, EventArgs e)
        {
            AudioDjStudio.enumPlayerStatus status = audioDjStudio1.GetPlayerStatus(Player);

            if (status != AudioDjStudio.enumPlayerStatus.SOUND_NONE)
            {
                if (status == AudioDjStudio.enumPlayerStatus.SOUND_PAUSED)
                {
                    audioDjStudio1.ResumeSound(Player);
                }
                else
                {
                    audioDjStudio1.PauseSound(Player);
                }
            }
        }

        private void pauseButtonPictureBox_MouseEnter(object sender, EventArgs e)
        {
            pauseButtonPictureBox.Image = global::Euphonics.Properties.Resources.pause_selected_32_32;
        }

        private void pauseButtonPictureBox_MouseLeave(object sender, EventArgs e)
        {
            pauseButtonPictureBox.Image = global::Euphonics.Properties.Resources.pause_normal_32_32;
        }

        private void stopButtonPictureBox_Click(object sender, EventArgs e)
        {
            audioDjStudio1.StopSound(Player);
        }

        private void stopButtonPictureBox_MouseEnter(object sender, EventArgs e)
        {
            stopButtonPictureBox.Image = global::Euphonics.Properties.Resources.stop_selected_32_32;
        }

        private void stopButtonPictureBox_MouseLeave(object sender, EventArgs e)
        {
            stopButtonPictureBox.Image = global::Euphonics.Properties.Resources.stop_normal_32_32;
        }

        private void increaseVolumePictureBox_MouseEnter(object sender, EventArgs e)
        {
            increaseVolumePictureBox.Image = global::Euphonics.Properties.Resources.increase_volume_selected;
        }

        private void increaseVolumePictureBox_MouseLeave(object sender, EventArgs e)
        {
            increaseVolumePictureBox.Image = global::Euphonics.Properties.Resources.increase_volume;
        }

        private void increaseVolumePictureBox_Click(object sender, EventArgs e)
        {
             if (volumeTrackBar.Value < maxVolume)
             {
                 volumeTrackBar.Value += 5;
                 audioDjStudio1.SetPlayerVolume(Player, (short) volumeTrackBar.Value);
             }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                // playListActive = true;

                openSongFileDialog.Filter =
                "Supported song formats (*.mp3;*.cda;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3)|" +
                "*.mp3;*.cda;*.mp2;*.wav;*.ogg;*.aiff;*.wma;*.wmv;*.asx;*.asf;" +
                "*.m4a;*.mp4;*.flac;*.aac;*.ac3;*.wv;" +
                "*.au;*.aif;*.w64;*.voc;*.sf;*.paf;*.pvf;*.caf;*.svx ;" +
                "*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3|" +
                "MP3 and MP2 songs (*.mp3;*.mp2)|*.mp3;*.mp2|" +
                "CD tracks (*.cda)|*.cda|" +
                "AAC and MP4 songs (*.aac;*.mp4)|*.aac;*.mp4|" +
                "OGG Vorbis songs (*.ogg)|*.ogg|" +
                "AIFF songs (*.aiff)|*.aiff|" +
                "AC3 songs (*.ac3)|*.ac3;|" +
                "ALAC songs (*.m4a)|*.ac3;|" +
                "FLAC songs (*.flac)|*.flac;|" +
                "MOD music (*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3)|*.it;*.xm;*.s3m;*.mod;*.mtm;*.mo3|" +
                "WAV songs (*.wav)|*.wav|" +
                "WavPack songs (*.wv)|*.wv;|" +
                "Windows Media songs (*.wma;*.wmv;*.asx;*.asf)|*.wma;*.wmv;*.asx;*.asf|" +
                "All files (*.*)|*.*";

                int index;
               
                if (openSongFileDialog.ShowDialog() == DialogResult.OK)
                {
                    this.fileName = openSongFileDialog.FileName;
                    index = this.fileName.LastIndexOf('\\');
                    this.songName = this.fileName.Substring(index + 1);
                }
                else// if (openSongFileDialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }

                if (fileName != null)
                {
                    audioDjStudio1.PlayListAddItem(Player, fileName, ++playListIndex);
                    fillPlayListView();
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry, Euphonics does not support that particular file. Please choose another song!" + " (The technical problem is: " + ex.Message + ".)");
            } 
        }
        
        private void playButton_Click(object sender, EventArgs e)
        {
            playListActive = true;
            playHighlightedSong();
        }

        private void playListView_DoubleClick(object sender, EventArgs e)
        {
            playListActive = true;
            playHighlightedSong();         
        }

        private void playHighlightedSong()
        {
            ListView.SelectedIndexCollection indices = playListView.SelectedIndices;

            if (indices.Count == 0)
            {
                audioDjStudio1.PlayListExecute(Player, true);
                return;
            }

            short location = (short)indices[0];
            audioDjStudio1.PlayListExecAt(Player, location);
        }

        private void loopButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            if (!loop)
            {
                audioDjStudio1.PlayListSetLoop(Player, true);
                loopButton.Image = global::Euphonics.Properties.Resources.buttonBackgroundSelected;
                loop = true;
            }
            else
            {
                audioDjStudio1.PlayListSetLoop(Player, false);
                loopButton.Image = global::Euphonics.Properties.Resources.buttonBackground;
                loop = false;
            }
        }

        private void loadButton_Click(object sender, EventArgs e)
        {
            // playListActive = true;
            
            openSongFileDialog.Filter = "Supported playlist formats: (*.m3u;*.pls;*.wpl)|*.m3u;*.pls;*.wpl|M3U PlayList (*.m3u)|*.m3u|PLS Playlist (*.pls)|*.pls|WPL Playlist (*.wpl)|*.wpl|All file (*.*)|*.*";

            if (openSongFileDialog.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            loadPlaylist(sender, e, openSongFileDialog.FileName);
        }

        private void loadPlaylist(object sender, EventArgs e, string playlistFile)
        {
            playListActive = true;

            // showPLAndEQButton_Click(sender, e);

            // Show the playlist and equalizer
            this.Width = bigWidth;
            showPlayList();
            showEqualizer();
            showPlayListAndEqualizer = true;
            playlistAndEqualizerToolStripMenuItem.Checked = true;

            clearListView();
            clearPlaylist();

            audioDjStudio1.PlayListLoad(Player, playlistFile, AudioDjStudio.enumPlayListModes.PLAYLIST_FULL_MODE);
            displayPlayList();

            // Execute the playlist from the beginning
            audioDjStudio1.PlayListExecute(Player, true);
        }

        private void clearListView()
        {
            int count = playListView.Items.Count;

            for (int i = count - 1; i >= 0; i--)
            {
                playListView.Items.RemoveAt(i);
            }
        }

        private void clearPlaylist()
        {
            short songCount = audioDjStudio1.PlayListGetCount(Player);
            
            // Remove songs from the back of the playlist.
            // (For some reason, this works properly; whereas
            // removing from the front doesn't.)
            for (short i = songCount; i >= 0; --i)
            {
                audioDjStudio1.PlayListRemoveItem(Player, i);
            }            
        }

        private void displayPlayList()
        {
            int count = audioDjStudio1.PlayListGetCount(Player);
            playListIndex = (short) (count - 1);

            for (short index = 0; index < count; index++)
            {
                string titleStr = audioDjStudio1.PlayListGetItemString(Player, index, AudioDjStudio.enumPlayListStrings.PL_STRING_INFO_TITLE);
                string durationStr = audioDjStudio1.PlayListGetItemString(Player, index, AudioDjStudio.enumPlayListStrings.PL_STRING_INFO_FORMATTED_DURAT_NO_MS);
                
                if (titleStr.Length == 0)
                {
                    titleStr = audioDjStudio1.PlayListGetItemString(Player, index, AudioDjStudio.enumPlayListStrings.PL_STRING_FILENAME);
                }                
              
                string[] stringList = new string[] { titleStr, durationStr };

                ListViewItem item = new ListViewItem(stringList);
                playListView.Items.Add(item);                
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            try
            {
                playListActive = true;

                savePlayListDialog.Title = "Save Playlist";
                savePlayListDialog.DefaultExt = "m3u";
                savePlayListDialog.Filter =
                "Playlist formats (*.m3u;*.pls;*.wpl)|*.m3u;*.pls;*.wpl|" +
                "M3U (*.m3u)|*.m3u|" +
                "PLS (*.pls)|*.pls|" +
                "WPL (*.wpl)|*.wpl|" +
                "All files (*.*)|*.*";

                if (savePlayListDialog.ShowDialog() == DialogResult.OK)
                {
                    string fileName = savePlayListDialog.FileName;
                    int index = fileName.LastIndexOf('.');

                    // Note that the default extension is "m3u" (see above).
                    string extension = fileName.Substring(index + 1);

                    // Playlists will be saved in the "m3u" format by default.
                    AudioDjStudio.enumPlayListFormats format = AudioDjStudio.enumPlayListFormats.PLAYLIST_FORMAT_M3U;
                                                                                             
                    switch (extension.ToLower())
                    {
                        case "pls":
                            format = AudioDjStudio.enumPlayListFormats.PLAYLIST_FORMAT_PLS;
                            break;
                        case "wpl":
                            format = AudioDjStudio.enumPlayListFormats.PLAYLIST_FORMAT_WPL;
                            break;
                        default:
                            format = AudioDjStudio.enumPlayListFormats.PLAYLIST_FORMAT_M3U;
                            break;
                    }

                    audioDjStudio1.PlayListSave(Player, fileName, format);
                    
                }
                else// if (savePlayListDialog.ShowDialog() == DialogResult.Cancel)
                {
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error saving the playlist." + " (The technical problem is: " + ex.Message + ".)");
            } 
        }

        private void shuffleButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            if (!shuffle)
            {
                audioDjStudio1.PlayListSetShuffle(Player, true);
                shuffleButton.Image = global::Euphonics.Properties.Resources.buttonBackgroundSelected;
                shuffle = true;
            }
            else
            {
                audioDjStudio1.PlayListSetShuffle(Player, false);
                shuffleButton.Image = global::Euphonics.Properties.Resources.buttonBackground;
                shuffle = false;
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            ListView.SelectedIndexCollection indices = playListView.SelectedIndices;
                        
            if (indices.Count == 0)
            {
                return;
            }

            short location = (short)indices[0];
            audioDjStudio1.PlayListRemoveItem(Player, location);
            playListView.Items.RemoveAt(location);
            playListIndex--;
        }

        private void moveUpButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            ListView.SelectedIndexCollection indices = playListView.SelectedIndices;
                        
            if (indices.Count == 0)
            {
                playListView.Select();
                return;
            }

            short location = (short)indices[0];
            short above = (short)(location - 1);

            if (above >= 0)
            {
                audioDjStudio1.PlayListMoveItem(Player, location, above);
                clearListView();
                displayPlayList();
                playListView.Items[above].Selected = true;
                playListView.Items[above].EnsureVisible();
                playListView.Select();
            }
        }

        private void moveDownButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            ListView.SelectedIndexCollection indices = playListView.SelectedIndices;

            if (indices.Count == 0)
            {
                playListView.Select();
                return;
            }

            short location = (short)indices[0];
            short below = (short)(location + 1);

            if (below < playListView.Items.Count)
            {
                audioDjStudio1.PlayListMoveItem(Player, location, below);
                clearListView();
                displayPlayList();
                playListView.Items[below].Selected = true;
                playListView.Items[below].EnsureVisible();
                playListView.Select(); ;
            }
        }

        private void createEqualizer()
        {
            if (audioDjStudio1.IsDirectX8Available())
            {
                audioDjStudio1.EqualizerBandAdd(Player, 80, bandwidth, 0);
                audioDjStudio1.EqualizerBandAdd(Player, 310, bandwidth, 0);
                audioDjStudio1.EqualizerBandAdd(Player, 1000, bandwidth, 0);
                audioDjStudio1.EqualizerBandAdd(Player, 3100, bandwidth, 0);
                audioDjStudio1.EqualizerBandAdd(Player, 10000, bandwidth, 0);
            }
            else
            {
                MessageBox.Show("Sorry! You will need to install DirectX version 8 or higher for the equalizer to work properly.");
            }
        }

        private void trackBar80Hz_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.EqualizerBandSetGain(Player, 80, ((float) trackBar80Hz.Value / 100.0f));
        }

        private void trackBar310Hz_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.EqualizerBandSetGain(Player, 310, ((float)trackBar80Hz.Value / 100.0f));
        }

        private void trackBar1000Hz_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.EqualizerBandSetGain(Player, 1000, ((float)trackBar80Hz.Value / 100.0f));
        }

        private void trackBar3100Hz_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.EqualizerBandSetGain(Player, 3100, ((float)trackBar80Hz.Value / 100.0f));
        }

        private void trackBar10000Hz_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.EqualizerBandSetGain(Player, 10000, ((float)trackBar80Hz.Value / 100.0f));
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            audioDjStudio1.SetTempoPerc(Player, 0.0f);
            tempoTrackBar.Value = 0;
            
            audioDjStudio1.SetPitch(Player, 0);
            pitchTrackBar.Value = 0;
            
            audioDjStudio1.EqualizerReset(Player);
            trackBar80Hz.Value = 0;
            trackBar310Hz.Value = 0;
            trackBar1000Hz.Value = 0;
            trackBar3100Hz.Value = 0;
            trackBar10000Hz.Value = 0;
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            audioDjStudio1.PlayListExecNext(Player);
        }

        private void prevButton_Click(object sender, EventArgs e)
        {
            playListActive = true;

            audioDjStudio1.PlayListExecPrev(Player);
        }

        private void tempoTrackBar_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.SetTempoPerc(Player, (float)tempoTrackBar.Value);
        }

        private void pitchTrackBar_Scroll(object sender, EventArgs e)
        {
            audioDjStudio1.SetPitch(Player, (short)pitchTrackBar.Value);
        }

        private void showPlayList()
        {
            // Playlist controls
            playListView.Show();
            loadButton.Show();
            addButton.Show();
            playButton.Show();
            saveButton.Show();
            nextButton.Show();
            prevButton.Show();
            shuffleButton.Show();
            loopButton.Show();
            deleteButton.Show();
            moveDownButton.Show();
            moveUpButton.Show();
            clearButton.Show();
        }
        
        private void showEqualizer()
        {
            // Equalizer controls
            tempoTrackBar.Show();
            tempoLabel.Show();
            pitchTrackBar.Show();
            pitchLabel.Show();
            trackBar80Hz.Show();
            label80Hz.Show();
            trackBar310Hz.Show();
            label310Hz.Show();
            trackBar1000Hz.Show();
            label1000Hz.Show();
            trackBar3100Hz.Show();
            label3100Hz.Show();
            trackBar10000Hz.Show();
            label10000Hz.Show();
            resetButton.Show();
        }

        private void hidePlayList()
        {
            // Playlist controls
            playListView.Hide();
            loadButton.Hide();
            addButton.Hide();
            playButton.Hide();
            saveButton.Hide();
            nextButton.Hide();
            prevButton.Hide();
            shuffleButton.Hide();
            loopButton.Hide();
            deleteButton.Hide();
            moveDownButton.Hide();
            moveUpButton.Hide();
            clearButton.Hide();
        }

        private void hideEqualizer()
        {
            // Equalizer controls
            tempoTrackBar.Hide();
            tempoLabel.Hide();
            pitchTrackBar.Hide();
            pitchLabel.Hide();
            trackBar80Hz.Hide();
            label80Hz.Hide();
            trackBar310Hz.Hide();
            label310Hz.Hide();
            trackBar1000Hz.Hide();
            label1000Hz.Hide();
            trackBar3100Hz.Hide();
            label3100Hz.Hide();
            trackBar10000Hz.Hide();
            label10000Hz.Hide();
            resetButton.Hide();
        }

        private void spectrumButton_Click(object sender, EventArgs e)
        {
            audioDjStudio1.DisplayOscilloscope.Show(Player, false);
            show_Spectrum();
            spectrumFeedbackToolStripMenuItem.Checked = true;
            oscilloscopeFeedbackToolStripMenuItem.Checked = false;
        }

        private void oscilloscopeButton_Click(object sender, EventArgs e)
        {
            audioDjStudio1.DisplaySpectrum.Show(Player, false);
            show_Oscilloscope();
            oscilloscopeFeedbackToolStripMenuItem.Checked = true;
            spectrumFeedbackToolStripMenuItem.Checked = false;
        }

        private void showPLAndEQButton_Click(object sender, EventArgs e)
        {
            if (showPlayListAndEqualizer == false)
            {
                this.Width = bigWidth;
                showPlayList();
                showEqualizer();
                showPlayListAndEqualizer = true;
                playlistAndEqualizerToolStripMenuItem.Checked = true;
            }
            else
            {
                this.Width = smallWidth;
                hidePlayList();
                hideEqualizer();
                showPlayListAndEqualizer = false;
                playlistAndEqualizerToolStripMenuItem.Checked = false;
            }
        }

        private void decreaseVolumeButtonPictureBox_Click(object sender, EventArgs e)
        {
            if (volumeTrackBar.Value > minVolume)
            {
                volumeTrackBar.Value -= 5;
                audioDjStudio1.SetPlayerVolume(Player, (short)volumeTrackBar.Value);
            }
        }

        private void decreaseVolumeButtonPictureBox_MouseEnter(object sender, EventArgs e)
        {
            decreaseVolumeButtonPictureBox.Image = global::Euphonics.Properties.Resources.decreaseVolumeSelected;
        }

        private void decreaseVolumeButtonPictureBox_MouseLeave(object sender, EventArgs e)
        {
            decreaseVolumeButtonPictureBox.Image = global::Euphonics.Properties.Resources.decreaseVolume;
        }

        private void button_MouseEnter(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            b.Image = global::Euphonics.Properties.Resources.buttonBackgroundSelected;
        }

        private void button_MouseLeave(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            b.Image = global::Euphonics.Properties.Resources.buttonBackground;            
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            clearListView();
            clearPlaylist();
            playListIndex = -1;         

            if (playListActive == true)
            {                
                audioDjStudio1.StopSound(Player);
            }

            playListActive = false;
        }

        private void loopButton_MouseEnter(object sender, EventArgs e)
        {
            if (loop)
            {
                return;
            }
            else
            {
                loopButton.Image = global::Euphonics.Properties.Resources.buttonBackgroundSelected;
            }
        }

        private void loopButton_MouseLeave(object sender, EventArgs e)
        {
            if (loop)
            {
                return;
            }
            else
            {
                loopButton.Image = global::Euphonics.Properties.Resources.buttonBackground;
            }
        }

        private void shuffleButton_MouseEnter(object sender, EventArgs e)
        {
            if (shuffle)
            {
                return;
            }
            else
            {
                shuffleButton.Image = global::Euphonics.Properties.Resources.buttonBackgroundSelected;
            }
        }

        private void shuffleButton_MouseLeave(object sender, EventArgs e)
        {
            if (shuffle)
            {
                return;
            }
            else
            {
                shuffleButton.Image = global::Euphonics.Properties.Resources.buttonBackground;
            }
        }

        private void soundLoadedEvent(object sender, AudioDjStudio.SoundLoadedEventArgs e)
        {
            if(playListActive)
            {
                string titleStr = audioDjStudio1.PlayListGetItemString(Player, e.nItemIndex, AudioDjStudio.enumPlayListStrings.PL_STRING_INFO_TITLE);
            
                if (titleStr.Length == 0)
                {
                    titleStr = audioDjStudio1.PlayListGetItemString(Player, playListIndex, AudioDjStudio.enumPlayListStrings.PL_STRING_FILENAME);
                }

                display_SongInfo(titleStr);
                songName = titleStr;

                int count = playListView.Items.Count;
                for (int i = count - 1; i >= 0; i--)
                {
                    playListView.Items[i].Selected = false;
                }

                playListView.Items[e.nItemIndex].Selected = true;
                playListView.Items[e.nItemIndex].EnsureVisible();
                playListView.Select();
            }           
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openButtonPictureBox_Click(sender, e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void spectrumFeedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            spectrumButton_Click(sender, e);            
        }

        private void oscilloscopeFeedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            oscilloscopeButton_Click(sender, e);            
        }

        private void playlistAndEqualizerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showPLAndEQButton_Click(sender, e);           
        }

        private void playToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            playButtonPictureBox_Click(sender, e);
        }

        private void pauseUnpauseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pauseButtonPictureBox_Click(sender, e);
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            audioDjStudio1.StopSound(Player);
        }

        private void volumeUpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            increaseVolumePictureBox_Click(sender, e);
        }

        private void volumeDownToolStripMenuItem_Click(object sender, EventArgs e)
        {
            decreaseVolumeButtonPictureBox_Click(sender, e);
        }

        private void loadPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
        {
            showPLAndEQButton_Click(sender, e);
            loadButton_Click(sender, e);
        }

        private void aboutEuphonicsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox about = new AboutBox();
            about.Show();        
        }

        private void MainForm_DragEnter(object sender, DragEventArgs e)
        {
            // The following segment of code is adapted from
            // http://support.microsoft.com/kb/307966
            if(e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            //else
            //{
            //    e.Effect = DragDropEffects.None;
            //}
        }

        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop);
            for (int i = 0; i < s.Length; i++)
            {
                process(sender, e, s[i]);
                // playSong(s[i]);                
            }
        }

        // This method is called when a file is dragged and dropped onto
        // the main form. It is also used when a file is double-clicked
        // and that file's file type has already been associated with "Euphonics".
        private void playSong(string fileName)
        {
            try
            {
                int index;
                
                playListActive = false;
                
                if (fileName != null)
                {
                    index = fileName.LastIndexOf('\\');
                    this.songName = fileName.Substring(index + 1);

                    audioDjStudio1.LoadSound(Player, fileName);
                    audioDjStudio1.PlaySound(Player);

                    if (this.songName != null)
                    {
                        display_SongInfo(this.songName);
                    }
                    else
                    {
                        display_SongInfo(fileName);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sorry, Euphonics does not support that particular file. Please choose another song!" + " (The technical problem is: " + ex.Message + ".)");
            } 
        }
    }
}
